package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.User;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.mongo.Friend;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessageService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    /**
     * 返回环信用户信息
     * @return
     */
    public HuanXinUserVo getUserMessage() {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //根据当前用户id查询环信用户信息
        User user = userApi.findById(userId);
        //返回
        if(user == null){
            return null;
        }
        return new HuanXinUserVo(user.getHxUser(), user.getHxPassword());
    }

    /**
     * 通过环信id查询用户详情
     * @param huanxinId
     * @return
     */
    public UserInfoVo getHXUserInfo(String huanxinId) {
        //通过环信id查询用户
        User user = userApi.findByHXId(huanxinId);
        //获取用户id
        Long userId = user.getId();
        //使用用户id查询userInfo
        UserInfo info = userInfoApi.findById(userId);
        //拼接vo对象返回
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(info,vo);
        if(info.getAge() != null){
            vo.setAge(info.getAge().toString());
        }
        return vo;
    }

    /**
     * 联系人添加
     * @param friendId
     */
    public void contacts(Long friendId) {
        //调用环信实现联系人添加
        Long userId = UserHolder.getUserId();
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        //未添加成功则抛出错误
        if(!aBoolean){
            throw  new BusinessException(ErrorResult.error());
        }
        //在friend表添加好友数据
        friendApi.contacts(userId,friendId);
    }

    /**
     * 分页查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult getContactsList(Integer page, Integer pagesize, String keyword) {
        //根据api查询当前用户的好友数据
        List<Friend> friendList = friendApi.getContactsList(UserHolder.getUserId(),page,pagesize);
        //提取数据列表中的好友id
        if(friendList == null || friendList.size() == 0){
            return new PageResult();
        }
        //调用userInfoApi查询好友的用户详情
        List<Long> ids = CollUtil.getFieldValues(friendList, "friendId", Long.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids, userInfo);
        //构造vo对象
        ArrayList<ContactVo> list = new ArrayList();
        for (Friend friend : friendList) {
            UserInfo info = map.get(friend.getFriendId());
            if(info != null){
                ContactVo vo = ContactVo.init(info);
                list.add(vo);
            }
        }
        //设置items并返回
        return new PageResult(page,pagesize,0L,list);
    }
}
